package mysql

import (
	"gitee.com/hexug/go-tools/ioc/log"
	_ "github.com/go-sql-driver/mysql"
	"gorm.io/gorm"
	"sync"
)

type Mysql struct {
	Host     string `toml:"host" yaml:"host" env:"MYSQL_HOST"`
	Port     int    `toml:"port" yaml:"port" env:"MYSQL_PORT"`
	User     string `toml:"user" yaml:"user" env:"MYSQL_USER"`
	Password string `toml:"password" yaml:"password" env:"MYSQL_PASSWORD"`
	Database string `toml:"database" yaml:"database" env:"MYSQL_DATABASE"`
	// 编码
	Charset   string `toml:"charset" yaml:"charset" env:"DB_CHARSET"`
	ParseTime string `toml:"parse_time" yaml:"parse_time" env:"DB_PARSE_TIME"`
	// 是否可以执行多条sql语句
	MultiStateMents string `toml:"multi_state_ments" yaml:"multi_state_ments" env:"MYSQL_MULTISTATEMENTS"`
	// 最大连接数
	MaxOpenConn int `toml:"max_open_conn" yaml:"max_open_conn" env:"MYSQL_MAXOPENCONN"`
	// 最大空闲数
	MaxIdleConn int `toml:"max_idle_conn" yaml:"max_idle conn" env:"MYSQL_MAXIDLECONN"`

	// 高级配置
	ConnMaxLifeTime int `toml:"conn_max_life_time" yaml:"conn_max_life_time" env:"MYSQL_CONNMAXLIFETIME"`
	ConnMaxIdleTime int `toml:"conn_max_idle_time" yaml:"conn_max_idle_time" env:"MYSQL_CONNMAXIDLETIME"`
	TimeOut         int `toml:"timeout" yaml:"timeout" env:"MYSQL_TIMEOUT"`

	db   *gorm.DB
	lock sync.Mutex
	log  *log.Log
}

func NewMysql() *Mysql {
	return &Mysql{
		Host:            "localhost",
		Port:            3306,
		User:            "root",
		Password:        "111111",
		Database:        "test",
		Charset:         "utf8mb4",
		ParseTime:       "True",
		MultiStateMents: "True",
		MaxOpenConn:     50,
		MaxIdleConn:     10,
		TimeOut:         5,
	}
}
